package com.cs.bd.ad.params;

import android.content.Context;
import android.text.TextUtils;

import com.cs.bd.ad.AdSdkApi;
import com.cs.bd.ad.bean.AdModuleInfoBean;
import com.cs.bd.ad.http.bean.BaseModuleDataItemBean;
import com.cs.bd.ad.manager.AdSdkManager;
import com.cs.bd.ad.sdk.AdmobAdConfig;
import com.cs.bd.ad.sdk.FacebookAdConfig;
import com.cs.bd.ad.sdk.MoPubAdConfig;

import java.util.HashMap;

public class AdSdkParamsBuilder {

    static final long DEFAULT_TIMEOUT = 30000L;
    public Context mContext;
    public final int mVirtualModuleId;
    public final int mReturnAdCount;
    public final boolean mIsNeedDownloadIcon;
    public final boolean mIsNeedDownloadBanner;
    /** @deprecated */
    public final boolean mIsNeedPreResolve;
    public final boolean mIsPreResolveBeforeShow;
    public final boolean mIsRequestData;
    public final boolean mIsAddFilterPackageNames;
    public final String mBuyuserchannel;
    public final Integer mUserFrom;
    public final Integer mCdays;
    public final String mTabCategory;
    public final boolean mNeedShownFilter;
    public final AdSdkManager.ILoadAdvertDataListener mLoadAdvertDataListener;
    public final AdSdkManager.IAdControlInterceptor mAdControlInterceptor;
    public final FacebookAdConfig mFacebookAdConfig;
    public final AdmobAdConfig mAdmobAdConfig;
    public final MoPubAdConfig mMoPubAdConfig;
    private int mShownCount;
    public final boolean mUseThreadPool;
    public final long mTimeOut;
    public final boolean mIsUploadClientAdRequest;
    public final int[] mFilterAdCacheTags;
    public final boolean mApplyAdCache;
    public final boolean mIsUploadAdRequestStatistic;
    public final boolean mDetectVpn;
    public final int mPosition;
    public final String mAppMonetApplicationId;
    public final boolean isChargeLocker;

    public int getShownCount() {
        return this.mShownCount;
    }

    private AdSdkParamsBuilder(AdSdkParamsBuilder.Builder builder) {
        this.mShownCount = -1;
        this.mContext = builder.mContext;
        this.mVirtualModuleId = builder.mVirtualModuleId;
        this.mReturnAdCount = builder.mReturnAdCount;
        this.mIsNeedDownloadIcon = builder.mIsNeedDownloadIcon;
        this.mIsNeedDownloadBanner = builder.mIsNeedDownloadBanner;
        this.mIsNeedPreResolve = builder.mIsNeedPreResolve;
        this.mIsPreResolveBeforeShow = builder.mIsPreResolveBeforeShow;
        this.mIsRequestData = builder.mIsRequestData;
        this.mIsAddFilterPackageNames = builder.mIsAddFilterPackageNames;
        this.mBuyuserchannel = builder.mBuyuserchannel;
        int cdays = builder.mCdays == null ? 0 : builder.mCdays;
        long installTime = System.currentTimeMillis() - (long)cdays * 86400000L;
//        if (cdays <= 0) {
//            long[] ti = AppUtils.getPkgTimeInfo(this.mContext, this.mContext.getPackageName());
//            installTime = ti != null ? ti[0] : installTime;
//        }

        this.mCdays = AdSdkApi.calculateCDays(this.mContext, installTime);
        this.mTabCategory = builder.mTabCategory;
        this.mShownCount = builder.mShownCount;
        this.mNeedShownFilter = builder.mNeedShownFilter;
        this.mLoadAdvertDataListener = builder.mLoadAdvertDataListener;
        this.mAdControlInterceptor = builder.mAdControlInterceptor;
        this.mFacebookAdConfig = builder.mFacebookAdConfig;
        this.mAdmobAdConfig = builder.mAdmobAdConfig;
        this.mMoPubAdConfig = builder.mMoPubAdConfig;
        this.mUseThreadPool = builder.mUseThreadPool;
        if (builder.mApplyAdCache) {
            this.mTimeOut = 30000L == builder.mTimeOut ? 3000L : builder.mTimeOut;
        } else {
            this.mTimeOut = builder.mTimeOut;
        }

        this.mIsUploadClientAdRequest = builder.mIsUploadClientAdRequest;
        this.mFilterAdCacheTags = builder.mFilterAdCacheTags;
        this.mApplyAdCache = builder.mApplyAdCache;
        this.mIsUploadAdRequestStatistic = builder.mIsUploadAdRequestStatistic;
        this.mUserFrom = builder.mUserFrom;
        this.mDetectVpn = builder.mDetectVpn;
        this.mPosition = builder.mPosition;
        this.mAppMonetApplicationId = builder.mAppMonetApplicationId;
        this.isChargeLocker = builder.mIsChargeLocker;
    }

    public final boolean checkFilterAndSupportAdvs(BaseModuleDataItemBean moduleDataItemBean) {
        return true;
    }

    public final boolean commonLoadCondition(BaseModuleDataItemBean moduleDataItemBean) {
        return !AdModuleInfoBean.isContainValue(this.mFilterAdCacheTags, moduleDataItemBean.getAdCacheFlag());
    }

    public static AdSdkParamsBuilder.Builder createEmptyBuilder(String buyChannel, Integer cdays, Integer userFrom) {
        return (new AdSdkParamsBuilder.Builder()).buyuserchannel(buyChannel).cdays(cdays).userFrom(userFrom);
    }

    public static class Builder {
        private Context mContext;
        private int mVirtualModuleId;
        private int mReturnAdCount;
        private boolean mIsNeedDownloadIcon;
        private boolean mIsNeedDownloadBanner;
        private boolean mIsNeedPreResolve;
        private boolean mIsPreResolveBeforeShow;
        private boolean mIsRequestData;
        private boolean mIsAddFilterPackageNames;
        private String mBuyuserchannel;
        private Integer mUserFrom;
        private Integer mCdays;
        private String mTabCategory;
        private int mShownCount;
        private boolean mNeedShownFilter;
        private AdSdkManager.ILoadAdvertDataListener mLoadAdvertDataListener;
        private AdSdkManager.IAdControlInterceptor mAdControlInterceptor;
        private FacebookAdConfig mFacebookAdConfig;
        private AdmobAdConfig mAdmobAdConfig;
        private MoPubAdConfig mMoPubAdConfig;
        private boolean mUseThreadPool;
        private long mTimeOut;
        public boolean mIsUploadClientAdRequest;
        private int[] mFilterAdCacheTags;
        private boolean mApplyAdCache;
        private boolean mIsUploadAdRequestStatistic;
        private boolean mDetectVpn;
        private int mPosition;
        private String mAppMonetApplicationId;
        public boolean mIsChargeLocker;

        /** @deprecated */
        public Builder(Context context, int virtualModuleId, String tabCategory, AdSdkManager.ILoadAdvertDataListener loadAdvertDataListener) {
            this.mReturnAdCount = 0;
            this.mIsAddFilterPackageNames = true;
            this.mUserFrom = null;
            this.mShownCount = -1;
            this.mNeedShownFilter = true;
            this.mUseThreadPool = false;
            this.mTimeOut = 30000L;
            this.mIsUploadClientAdRequest = false;
            this.mFilterAdCacheTags = null;
            this.mApplyAdCache = false;
            this.mIsUploadAdRequestStatistic = true;
            this.mDetectVpn = true;
            this.mPosition = -1;
            this.mContext = context;
            this.mVirtualModuleId = virtualModuleId;
            this.mTabCategory = TextUtils.isEmpty(tabCategory) ? String.valueOf(virtualModuleId) : tabCategory;
            this.mLoadAdvertDataListener = loadAdvertDataListener;
        }

        public Builder(Context context, int virtualModuleId, String buyChannel, Integer userFrom, String tabCategory, AdSdkManager.ILoadAdvertDataListener loadAdvertDataListener) {
            this.mReturnAdCount = 0;
            this.mIsAddFilterPackageNames = true;
            this.mUserFrom = null;
            this.mShownCount = -1;
            this.mNeedShownFilter = true;
            this.mUseThreadPool = false;
            this.mTimeOut = 30000L;
            this.mIsUploadClientAdRequest = false;
            this.mFilterAdCacheTags = null;
            this.mApplyAdCache = false;
            this.mIsUploadAdRequestStatistic = true;
            this.mDetectVpn = true;
            this.mPosition = -1;
            this.mContext = context;
            this.mVirtualModuleId = virtualModuleId;
            this.mBuyuserchannel = buyChannel;
            this.mUserFrom = userFrom;
            this.mTabCategory = TextUtils.isEmpty(tabCategory) ? String.valueOf(virtualModuleId) : tabCategory;
            this.mLoadAdvertDataListener = loadAdvertDataListener;
        }

        private Builder() {
            this.mReturnAdCount = 0;
            this.mIsAddFilterPackageNames = true;
            this.mUserFrom = null;
            this.mShownCount = -1;
            this.mNeedShownFilter = true;
            this.mUseThreadPool = false;
            this.mTimeOut = 30000L;
            this.mIsUploadClientAdRequest = false;
            this.mFilterAdCacheTags = null;
            this.mApplyAdCache = false;
            this.mIsUploadAdRequestStatistic = true;
            this.mDetectVpn = true;
            this.mPosition = -1;
        }

        public AdSdkParamsBuilder.Builder returnAdCount(int returnAdCount) {
            this.mReturnAdCount = returnAdCount;
            return this;
        }

        public AdSdkParamsBuilder.Builder isNeedDownloadIcon(boolean isNeedDownloadIcon) {
            this.mIsNeedDownloadIcon = isNeedDownloadIcon;
            return this;
        }

        public AdSdkParamsBuilder.Builder isNeedDownloadBanner(boolean isNeedDownloadBanner) {
            this.mIsNeedDownloadBanner = isNeedDownloadBanner;
            return this;
        }

        /** @deprecated */
        public AdSdkParamsBuilder.Builder isNeedPreResolve(boolean isNeedPreResolve) {
            this.mIsNeedPreResolve = false;
            return this;
        }

        public AdSdkParamsBuilder.Builder isPreResolveBeforeShow(boolean isPreResolveBeforeShow) {
            this.mIsPreResolveBeforeShow = isPreResolveBeforeShow;
            return this;
        }

        public AdSdkParamsBuilder.Builder isRequestData(boolean isRequestData) {
            this.mIsRequestData = isRequestData;
            return this;
        }

        public AdSdkParamsBuilder.Builder isAddFilterPackageNames(boolean isAddFilterPackageNames) {
            this.mIsAddFilterPackageNames = isAddFilterPackageNames;
            return this;
        }

        public AdSdkParamsBuilder.Builder supportAdTypeArray(Object supportAdObjectTypeArray) {
            return this;
        }

        public AdSdkParamsBuilder.Builder filterAdSourceArray(Object filterAdSourceArray) {
            return this;
        }

        public AdSdkParamsBuilder.Builder appMonetNoList(HashMap<String, Object> appMonetNoList) {
            return this;
        }

        public AdSdkParamsBuilder.Builder buyuserchannel(String buyuserchannel) {
            this.mBuyuserchannel = buyuserchannel;
            return this;
        }

        public AdSdkParamsBuilder.Builder userFrom(Integer userFrom) {
            this.mUserFrom = userFrom;
            return this;
        }

        public AdSdkParamsBuilder.Builder cdays(Integer cdays) {
            this.mCdays = cdays;
            return this;
        }

        public AdSdkParamsBuilder.Builder tabCategory(String tabCategory) {
            this.mTabCategory = tabCategory;
            return this;
        }

        public AdSdkParamsBuilder.Builder shownCount(int shownCount) {
            this.mShownCount = shownCount;
            return this;
        }

        public AdSdkParamsBuilder.Builder needShownFilter(boolean b) {
            this.mNeedShownFilter = b;
            return this;
        }

        public AdSdkParamsBuilder.Builder adControlInterceptor(AdSdkManager.IAdControlInterceptor interceptor) {
            this.mAdControlInterceptor = interceptor;
            return this;
        }

        public AdSdkParamsBuilder.Builder facebookAdConfig(FacebookAdConfig config) {
            this.mFacebookAdConfig = config;
            return this;
        }

        public AdSdkParamsBuilder.Builder admobAdConfig(AdmobAdConfig config) {
            this.mAdmobAdConfig = config;
            return this;
        }

        public AdSdkParamsBuilder.Builder moPubAdConfig(MoPubAdConfig config) {
            this.mMoPubAdConfig = config;
            return this;
        }

        public AdSdkParamsBuilder.Builder useThreadPool(boolean useThreadPool) {
            this.mUseThreadPool = useThreadPool;
            return this;
        }

        public AdSdkParamsBuilder.Builder fbTimeout(long timeout) {
            this.mTimeOut = Math.max(3000L, timeout);
            return this;
        }

        public AdSdkParamsBuilder.Builder isUploadClientAdRequest(boolean isUploadClientAdRequest) {
            this.mIsUploadClientAdRequest = isUploadClientAdRequest;
            return this;
        }

        public AdSdkParamsBuilder.Builder filterAdCacheTags(int[] filterAdCacheTags) {
            this.mFilterAdCacheTags = filterAdCacheTags;
            return this;
        }

        public AdSdkParamsBuilder.Builder applyAdCache(boolean b) {
            this.mApplyAdCache = b;
            return this;
        }

        public AdSdkParamsBuilder.Builder isUploadAdRequestStatistic(boolean b) {
            this.mIsUploadAdRequestStatistic = b;
            return this;
        }

        public AdSdkParamsBuilder.Builder detectVpn(boolean detect) {
            this.mDetectVpn = detect;
            return this;
        }

        public AdSdkParamsBuilder.Builder adPosition(int position) {
            this.mPosition = position;
            return this;
        }

        public AdSdkParamsBuilder.Builder appMonetApplicationId(String appMonetApplicationId) {
            this.mAppMonetApplicationId = appMonetApplicationId;
            return this;
        }

        public AdSdkParamsBuilder.Builder isChargeLocker(boolean isChargeLocker) {
            this.mIsChargeLocker = isChargeLocker;
            return this;
        }

        public AdSdkParamsBuilder build() {
            return new AdSdkParamsBuilder(this);
        }
    }

}
